package com.zh.work.service.platform.channel.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.work.dao.platform.channel.OnlineRouteMapper;
import com.zh.work.entity.channel.OnlineRouteEntity;
import com.zh.work.entity.channel.QrCodeRouteMerchEntity;
import com.zh.work.service.platform.channel.OnlineRouteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 网关路由规则设置业务逻辑实现层
 * @Author: Li.HuiNeng
 * @Create in: 2019/09/05
 * @Version: 1.0.1
 */
@Service
public class OnlineRouteServiceImpl extends ServiceImpl<OnlineRouteMapper, OnlineRouteEntity> implements OnlineRouteService {

    private static final Logger log = LoggerFactory.getLogger(OnlineRouteServiceImpl.class);

    @Autowired
    private OnlineRouteMapper mapper;


    @Override
    public IPage<OnlineRouteEntity> selectOnlineRoutePages(Page<OnlineRouteEntity> page, OnlineRouteEntity onlineRouteEntity) {
        return mapper.selectOnlineRoutePages(page, onlineRouteEntity.getBranchNo(), onlineRouteEntity.getMerchNo(),
                onlineRouteEntity.getStatus(), onlineRouteEntity.getRouteName());
    }

    @Transactional
    @Override
    public int putAddOnlineRoute(OnlineRouteEntity onlineRouteEntity) throws Exception {
        List<QrCodeRouteMerchEntity> list = JSON.parseArray(onlineRouteEntity.getOnlineMerchantJson(), QrCodeRouteMerchEntity.class);
        // 添加网关路由信息
        if (!onlineRouteEntity.insert()) return -2;
        if (list != null) {
            if (list.size() > 0) {
                list.forEach(f -> f.setRouteId(onlineRouteEntity.getId()));
                // 添加网关路由商户信息
                if (list.size() > 0) if (mapper.addOnlineRouteMerchant(list) <= 0) return -1;
            } else
                log.info("网关路由商户为空!");
        }
        return 1;
    }

    @Transactional
    @Override
    public int putModOnlineRouteById(OnlineRouteEntity onlineRouteEntity) throws Exception {
        List<QrCodeRouteMerchEntity> list = JSON.parseArray(onlineRouteEntity.getOnlineMerchantJson(), QrCodeRouteMerchEntity.class);
        // 查询数据库之前是否存在相关商户，不存在则直接添加，否则删除
        Map<String, Long> map = mapper.getOnlineMerchantNum(onlineRouteEntity.getId());
        if (map.get("num") != 0) {
            // 根据网关路由编号删除指定网关路由商户
            if (mapper.delOnlineMerchantById(onlineRouteEntity.getId()) <= 0) return -1;
        }
        if (list != null) {
            if (list.size() > 0) {
                // 添加网关路由商户
                list.forEach(f -> f.setRouteId(onlineRouteEntity.getId()));
                if (list.size() > 0) if (mapper.addOnlineRouteMerchant(list) <= 0) return -2;
            } else
                log.info("网关路由商户为空!");
        }
        // 修改网关路由规则信息
        if (!onlineRouteEntity.updateById()) return -3;
        return 1;
    }

    @Override
    public int putModOnlineRouteStatus(OnlineRouteEntity onlineRouteEntity) {
        return mapper.updateById(onlineRouteEntity);
    }

    @Override
    public List<QrCodeRouteMerchEntity> getOnlineMerchantByNo(Long id) {
        return mapper.getOnlineMerchantByNo(id);
    }
}
